package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.vo.CommonVo;

import com.cskaoyan.bean.MarketAftersale;
import com.cskaoyan.bean.admin.vo.Ids;
import com.cskaoyan.service.MarketAftersaleService;
import com.cskaoyan.utils.CommonUtil;
import com.cskaoyan.utils.Constant;
import com.github.pagehelper.PageHelper;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (MarketAftersale)表控制层
 *
 * @author makejava
 * @since 2021-10-18 22:15:17
 */
@RestController
@RequestMapping("admin/aftersale")
public class MarketAftersaleController {
    /**
     * 服务对象
     */
    @Resource
    private MarketAftersaleService marketAftersaleService;

    /**
     * 分页查询
     *
     * @param
     * @param
     * @return 查询结果
     */
    @GetMapping("list")
    @RequiresPermissions("admin:aftersale:list")
    public BaseRespVo queryByPage(String aftersaleSn,
                                  String orderId, BaseParam param, Integer status) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        Integer i = null;
        if (CommonUtil.isStrNotEmpty(orderId)) try {
            i = Integer.parseInt(orderId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return BaseRespVo.fail("参数不对");
        }
        String sn = null;
        if (aftersaleSn != null) sn = "%" + aftersaleSn + "%";
        List<MarketAftersale> marketAftersales = marketAftersaleService.queryByPage(sn, i, param, status);
        CommonVo marketAfterSaleVo = new CommonVo();
        int size = marketAftersales.size();
        marketAfterSaleVo.setList(marketAftersales);
        marketAfterSaleVo.setTotal(size);
        marketAfterSaleVo.setLimit(param.getLimit());
        marketAfterSaleVo.setPage(param.getPage());
        marketAfterSaleVo.setPages(size / param.getLimit() + 1);
        return BaseRespVo.ok(marketAfterSaleVo);
    }


    /**
     * 批量通过
     *
     * @return 编辑结果
     */
    @RequiresPermissions("admin:aftersale:batch-recept")
    @PostMapping("batch-recept")
    public BaseRespVo batchRecept(@RequestBody Ids ids) {
        Integer[] integers = ids.getIds();
        if (ids == null || integers.length == 0) return BaseRespVo.fail();
        int count = 0;
        for (int i = 0; i < integers.length; i++) {
            int recept = marketAftersaleService.recept(integers[i]);
            count+=recept;
        }
        if (count == 0) return BaseRespVo.fail("订单状态全都不可以系统通过");
        if (count < integers.length - 1) return BaseRespVo.fail("部分不成功");
        else return BaseRespVo.ok();
    }

    /**
     * 通过
     *
     * @return 编辑结果
     */
    @RequiresPermissions("admin:aftersale:recept")
    @PostMapping("recept")
    public BaseRespVo recept(@RequestBody MarketAftersale marketAftersale) {
        marketAftersaleService.recept(marketAftersale.getId());
        if (marketAftersale.getStatus() == Constant.UPDATE) return BaseRespVo.ok();
        else return BaseRespVo.fail();
    }

    /**
     * 拒绝
     *
     * @return 编辑结果
     */
    @RequiresPermissions("admin:aftersale:reject")
    @PostMapping("reject")
    public BaseRespVo reject(@RequestBody MarketAftersale marketAftersale) {

        marketAftersaleService.reject(marketAftersale.getId());
        if (marketAftersale.getStatus() == Constant.UPDATE) return BaseRespVo.ok();
        else return BaseRespVo.fail();
    }

    /**
     * 退款
     *
     * @return 编辑结果
     */
    @RequiresPermissions("admin:aftersale:refund")
    @PostMapping("refund")
    public BaseRespVo refund(@RequestBody MarketAftersale marketAftersale) {
        if (marketAftersale.getStatus()!= Constant.REFUND) return BaseRespVo.fail();
        marketAftersaleService.refund(marketAftersale.getId());
        return BaseRespVo.ok();
    }

    /**
     * 批量拒绝
     *
     * @return 编辑结果
     */
    @RequiresPermissions("admin:aftersale:batch-reject")
    @PostMapping("batch-reject")
    public BaseRespVo batchReject(@RequestBody Ids ids) {
        Integer[] integers = ids.getIds();
        if (ids == null || integers.length == 0) return BaseRespVo.fail();
        int count = 0;
        for (int i = 0; i < integers.length; i++) {
            int reject = marketAftersaleService.reject(integers[i]);
            count+=reject;
        }
        if (count == 0) return BaseRespVo.fail("订单状态全都不可以系统拒绝");
        if (count < integers.length - 1) return BaseRespVo.fail("部分不成功");
        else return BaseRespVo.ok();
    }


}

